import java.util.*;

/**
 * @author LKQ
 * @date 2022/5/8 20:47
 * @description 动态规划
 */
public class Solution3 {
    public static void main(String[] args) {

    }
    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        int m = obstacleGrid.length, n = obstacleGrid[0].length;
        // dp[i][j] 代表 到达位置 i，j的不同路径数量
        int[][] dp = new int[m][n];
        dp[0][0] = obstacleGrid[0][0] == 1 ? 0: 1;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (obstacleGrid[i][j] != 1) {
                    if (i > 0 && j > 0) {
                        dp[i][j] = dp[i-1][j] + dp[i][j-1];
                    }else if (i > 0) {
                        dp[i][j] = dp[i-1][j];
                    }else if (j > 0) {
                        dp[i][j] = dp[i][j-1];
                    }
                }
            }
        }
        return dp[m - 1][n - 1];
    }
}
